

export default {

    getDateRange(dateId) {
        // 范围 between；今天 date；昨天 yesterday；本周 week；上周 prevWeek；本月 month；上个月 prevMonth；半年度 halfYear；本年度 year；上年度 prevYear；
        var that = this;
        var now = new Date(); //当前日期
        var nowDayOfWeek = now.getDay(); //今天本周的第几天
        var nowDay = now.getDate(); //当前日
        var nowMonth = now.getMonth(); //当前月
        var nowYear = now.getYear(); //当前年
        nowYear += (nowYear < 2000) ? 1900 : 0;
        var lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        var lastYear = lastMonthDate.getYear();
        var lastMonth = lastMonthDate.getMonth();
        // 今天
        function todayDate() {
          var today = that.formatDate(now);
          return [today, today];
        }
        // 昨天
        function yesterdayDate() {
          var yesterday = new Date(nowYear, nowMonth, nowDay - 1)
          yesterday = that.formatDate(yesterday);
          return [yesterday, yesterday];
        }
        // 本周
        function thisWeekDate() {
          var weekStartDate = new Date(nowYear, nowMonth, (nowDay - nowDayOfWeek + 1));
          weekStartDate = that.formatDate(weekStartDate);
          var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
          weekEndDate = that.formatDate(weekEndDate);
          return [weekStartDate, weekEndDate]
        }
        // 上周
        function lastWeekDate() {
          var lastWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
          lastWeekStartDate = that.formatDate(lastWeekStartDate);
          var lastWeekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
          lastWeekEndDate = that.formatDate(lastWeekEndDate);
          return [lastWeekStartDate, lastWeekEndDate]
        }
        // 本月
        function thisMonthDate() {
          var monthStartDate = new Date(nowYear, nowMonth, 1);
          var monthEndDate = new Date(nowYear, nowMonth + 1, 1);
          var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
          monthStartDate = new Date(nowYear, nowMonth, 1);
          monthStartDate = that.formatDate(monthStartDate);
          monthEndDate = new Date(nowYear, nowMonth, days);
          monthEndDate = that.formatDate(monthEndDate);
          return [monthStartDate, monthEndDate]
        }
       
        function lastMouthDate() {
          
          var monthStartDate = new Date(nowYear , lastMonth, 1);
          var monthEndDate = new Date(nowYear, lastMonth + 1, 1);
          var lastMonthStartDate = '';
          var lastMonthEndDate = '';
         
          var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
          
          if( lastMonth == 11){
            lastMonthStartDate = new Date(nowYear - 1, lastMonth, 1);
          }else{
            lastMonthStartDate = new Date(nowYear , lastMonth, 1);
          }
           console.log(  lastMonthStartDate )
          lastMonthStartDate = that.formatDate(lastMonthStartDate);
          if( lastMonth == 11){
            lastMonthEndDate = new Date(nowYear - 1, lastMonth, days);
          }else{
            lastMonthEndDate = new Date(nowYear , lastMonth, days);
          }
         
          lastMonthEndDate = that.formatDate(lastMonthEndDate);
          
          return [lastMonthStartDate, lastMonthEndDate]
        }
        
        // 半年度
        function halfYearDate() {
        //   var halfYearEndDate = ''
          if (nowMonth <= 6) {
            var halfYearStartDate = new Date(nowYear, 0, 1);
            halfYearStartDate = that.formatDate(halfYearStartDate);
            var halfYearEndDate = new Date(nowYear, 5, 30);
            halfYearEndDate = that.formatDate(halfYearEndDate);
          } else {
            halfYearStartDate = new Date(nowYear, 6, 1);
            halfYearStartDate = that.formatDate(halfYearStartDate);
            halfYearEndDate = new Date(nowYear, 11, 31);
            halfYearEndDate = that.formatDate(halfYearEndDate);
          }
          return [halfYearStartDate, halfYearEndDate]
        }
        
        
        // 本年度
        function thisYearDate() {
          var nowYearStartDate = new Date(nowYear, 0, 1);
          nowYearStartDate = that.formatDate(nowYearStartDate);
          var nowYearEndDate = new Date(nowYear, 11, 31);
          nowYearEndDate = that.formatDate(nowYearEndDate);
          return [nowYearStartDate, nowYearEndDate]
        }
        // 上年度
        function lastYearDate() {
          var lastYearStartDate = new Date(nowYear - 1, 0, 1);
          lastYearStartDate = that.formatDate(lastYearStartDate);
          var lastYearEndDate = new Date(nowYear - 1, 11, 31);
          lastYearEndDate = that.formatDate(lastYearEndDate);
          return [lastYearStartDate, lastYearEndDate]
        }
        // 根据类型返回 开始日期和结束日期
        if (dateId == "date") {
          return todayDate();
        } else if (dateId == "yesterday") {
          return yesterdayDate();
        } else if (dateId == "week") {
          return thisWeekDate();
        } else if (dateId == "prevWeek") {
          return lastWeekDate();
        } else if (dateId == "month") {
          return thisMonthDate();
        } else if (dateId == "prevMonth") {
          return lastMouthDate();
        } else if (dateId == "halfYear") {
          return halfYearDate();
        } else if (dateId == "year") {
          return thisYearDate();
        } else if (dateId == "prevYear") {
          return lastYearDate();
        }
      },
      // 日期格式化
      formatDate: function(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return [year, month, day].map(this.formatNumber).join('-');
    },

     // 时间格式化
  formatTime: function(date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute, second].map(this.formatNumber)
      .join(':');
  },

  // 数字前加0 数字化
  formatNumber: function(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },

  // 当前时间 加 秒 分 时 日 月 年
  dateAdd: function(strInterval, Number) {
    var dtTmp = new Date();
    switch (strInterval) {
      case "s":
        return new Date(Date.parse(dtTmp) + (1000 * Number));
      case "n":
        return new Date(Date.parse(dtTmp) + (60000 * Number));
      case "h":
        return new Date(Date.parse(dtTmp) + (3600000 * Number));
      case "d":
        return new Date(Date.parse(dtTmp) + (86400000 * Number));
      case "w":
        return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
      case "q":
        return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(),
          dtTmp.getMinutes(),
          dtTmp.getSeconds());
      case "m":
        return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp
          .getMinutes(),
          dtTmp.getSeconds());
      case "y":
        return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp
          .getMinutes(),
          dtTmp.getSeconds());
    }
  }

    
}
  

